package com.aegis.manage.service.sys.login;

import cn.dev33.satoken.stp.StpUtil;
import com.aegis.common.utils.AssertUtil;
import com.aegis.core.model.AegisLoginInfo;
import com.aegis.core.utils.AegisUserUtil;
import com.aegis.manage.api.request.LoginRequest;
import com.aegis.manage.config.ManageProperties;
import com.aegis.manage.constants.ResponseStatusEnum;
import com.aegis.manage.model.dos.SysUser;
import com.aegis.manage.service.sys.permission.SysPermissionService;
import com.aegis.manage.service.sys.role.SysRoleService;
import com.aegis.manage.service.sys.user.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author wuweixin
 * 
 * @Version 1.0
 * @Descritube
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysPermissionService sysPermissionService;

    @Autowired
    private ManageProperties manageProperties;

    /**
     * 前端可视化页面的登录接口
     *
     * @param loginRequest
     * @return
     */
    @Override
    public AegisLoginInfo login(LoginRequest loginRequest) {
        SysUser sysUser = sysUserService.getUserByUsernameAndPwd(loginRequest.getUsername(), loginRequest.getPassword());
        AssertUtil.xAssert(sysUser == null, ResponseStatusEnum.USER_PWD_ERROR);
        AegisLoginInfo login = AegisUserUtil.login(sysUser.getId(), loginRequest.getUsername());
        AegisUserUtil.setUserGroup(sysUser.getId(), StpUtil.getRoleList());
        return login;
    }


}
